axios.defaults.baseURL = 'http://localhost:8080/'
Vue.prototype.$http = axios

//使用axios请求拦截器，让所有的请求都携带userToken
axios.interceptors.request.use(config => {
    //携带userToken
    let userToken =  localStorage.getItem("userToken");
    if(userToken){
        //添加请求头userToken
        config.headers['userToken'] = userToken;
    }
    return config;
},error => {
    Promise.reject(error);
})

//使用axios响应拦截器，处理未登录的请求
axios.interceptors.response.use(res => {
    console.log("jjjjjjj", res.data);
    let {success, msg} = res.data;
    if(!success && msg==="noUser"){
        alert("请先登录！");
        location.href = "/login.html";
    }
    return res;
},error => {
    Promise.reject(error);
})


//js代码获取GET请求地址栏中的?后面的请求参数值
var getQueryStringByName = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    var context = "";
    if (r != null)
        context = r[2];
    reg = null;
    r = null;
    return context == null || context == "" || context == "undefined" ? "" : context;
}